package com.tencreat.mall.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.core.domain.entity.SysUser;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.StringUtils;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.tencreat.domain.mall.MallGoods;
import com.tencreat.domain.mall.MallGoodsCut;
import com.tencreat.domain.mall.MallGoodsKill;
import com.tencreat.domain.mall.MallZoneGoods;
import com.tencreat.domain.pt.MallPtGoods;
import com.tencreat.service.mall.IMallGoodsCutService;
import com.tencreat.service.mall.IMallGoodsKillService;
import com.tencreat.service.mall.IMallGoodsService;
import com.tencreat.service.mall.IMallZoneGoodsService;
import com.tencreat.service.pt.IMallPtGoodsService;
import com.tencreat.service.system.impl.SysUserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 专区商品对应业务Controller
 *
 * @author wangkai
 * @date 2021-11-22
 */
@Api(tags = "专区商品对应业务api")
//@RestController
@RequestMapping("/mall/zoneGoods")
public class MallZoneGoodsController extends BaseController {
    //@Autowired
    private IMallZoneGoodsService mallZoneGoodsService;

    //@Autowired
    private IMallGoodsService mallGoodsService;

    //@Autowired
    private IMallGoodsKillService goodsKillService;

    //@Autowired
    private IMallPtGoodsService ptGoodsService;

    //Autowired
    private IMallGoodsCutService goodsCutService;
    //@Autowired
    private SysUserServiceImpl userService;

    /**
     * 查询专区商品对应业务列表
     */
    @ApiOperation(value = "查询专区商品对应业务列表")
    @PostMapping("/list")
    public HttpResult<IPage<MallZoneGoods>> list(@RequestBody MallZoneGoods mallZoneGoods) {
        Page<MallZoneGoods> page = new Page<>(mallZoneGoods.getPageNum(), mallZoneGoods.getPageSize());
        IPage<MallZoneGoods> iPage = mallZoneGoodsService.list2(page, mallZoneGoods);
        return HttpResult.ok(iPage);
    }

    /**
     * 导出专区商品对应业务列表
     */
    @ApiOperation(value = "导出专区商品对应业务列表")
    @PreAuthorize("@ss.hasPermi('system:goods:export')")
    @Log(title = "商品管理>商品专区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(MallZoneGoods mallZoneGoods) {
        List<MallZoneGoods> list = mallZoneGoodsService.selectMallZoneGoodsList(mallZoneGoods);
        ExcelUtil<MallZoneGoods> util = new ExcelUtil<MallZoneGoods>(MallZoneGoods.class);
        return util.exportExcel(list, "goods");
    }

    /**
     * 获取专区商品对应业务详细信息
     */
    @ApiOperation(value = "获取专区商品对应业务详细信息")
    @PreAuthorize("@ss.hasPermi('system:goods:query')")
    @PostMapping(value = "/getInfo")
    public HttpResult<MallZoneGoods> getInfo(@RequestBody ReqIdParam id) {
        return HttpResult.ok(mallZoneGoodsService.selectMallZoneGoodsById(id));
    }

    /**
     * 新增专区商品对应业务
     */
    @ApiOperation(value = "新增专区商品对应业务")
    @PreAuthorize("@ss.hasPermi('system:goods:add')")
    @Log(title = "商品管理>商品专区", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    @Transactional
    public HttpResult<Boolean> add(@RequestBody List<MallZoneGoods> mallZoneGoods) throws Exception {
        int sums = 0;
        for (MallZoneGoods mallZoneGood : mallZoneGoods) {
            Integer count = mallZoneGoodsService.lambdaQuery()
                    .eq(MallZoneGoods::getGoodsId, mallZoneGood.getGoodsId()).count();
            if (count != 0) {
                throw new Exception("该商品已经添加到专区,不能重复添加。商品名称:" + mallZoneGood.getGoodsName());
            }
            sums += mallZoneGoodsService.insertMallZoneGoods(mallZoneGood);
        }
        return to(sums);
    }

    /**
     * 修改专区商品对应业务
     */
    @ApiOperation(value = "修改专区商品对应业务")
    @PreAuthorize("@ss.hasPermi('system:goods:edit')")
    @Log(title = "商品管理>商品专区", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public HttpResult<Boolean> edit(@RequestBody MallZoneGoods mallZoneGoods) {
        return to(mallZoneGoodsService.updateMallZoneGoods(mallZoneGoods));
    }

    /**
     * 删除专区商品对应业务
     */
    @ApiOperation(value = "删除专区商品对应业务")
    @PreAuthorize("@ss.hasPermi('system:goods:remove')")
    @Log(title = "商品管理>商品专区", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public HttpResult<Boolean> remove(@RequestBody ReqIdsParam ids) {
        int count = goodsKillService.count(new LambdaQueryWrapper<MallGoodsKill>().eq(MallGoodsKill::getGoodsId, ids.getId().get(0)));
        if (count > 0) {
            return HttpResult.error("该商品参与了秒杀活动禁止删除");
        }
        count = ptGoodsService.count(new LambdaQueryWrapper<MallPtGoods>().eq(MallPtGoods::getGoodsId, ids.getId().get(0)));
        if (count > 0) {
            return HttpResult.error("该商品参与了拼团活动禁止删除");
        }
        count = goodsCutService.count(new LambdaQueryWrapper<MallGoodsCut>().eq(MallGoodsCut::getGoodsId, ids.getId().get(0)));
        if (count > 0) {
            return HttpResult.error("该商品参与了砍价活动禁止删除");
        }
        return to(mallZoneGoodsService.deleteMallZoneGoodsByIds(ids));
    }

    /*****
     *  将当前分类的排序减1 顺序上升一位
     * @param //mallCategory
     * @return
     */
    @PostMapping("/reSortOrderReduce")
    @Log(title = "商品管理>商品专区", businessType = BusinessType.UPDATE)
    public HttpResult<Boolean> reSortOrderReduce(@RequestBody MallZoneGoods mallZoneGoods){
        return HttpResult.ok(mallZoneGoodsService.reSortOrderReduce(mallZoneGoods));
    }

    /*****
     * 将当前分类的排序加1 顺序下降一位
     * @param //mallCategory
     * @return
     */
    @PostMapping("/reSortOrderPlus")
    @Log(title = "商品管理>商品专区", businessType = BusinessType.UPDATE)
    public HttpResult<Boolean> reSortOrderPlus(@RequestBody MallZoneGoods mallZoneGoods){
        return HttpResult.ok(mallZoneGoodsService.reSortOrderPlus(mallZoneGoods));
    }
}
